package feathers.controls
{
	import com.greensock.TweenLite;
	
	import feathers.data.ListCollection;
	import feathers.events.FeathersEventType;
	import feathers.events.ItemEvent;
	import feathers.skins.StandardIcons;
	
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.extensions.ClippedSprite;
	import starling.textures.Texture;
	

	public class ComboBox extends Sprite
	{
		public function ComboBox(){
			initFace();
		}
		
		private var iconImage:Image;
		private var button:Button;
		private var infoList:List;
		private function initFace():void
		{
			if(button == null){
				button = new Button();
				button.iconPosition = Button.HORIZONTAL_ALIGN_RIGHT;
//				button.label = 'color';
				addChild(button);
				var sp:Sprite = new Sprite();
				if(StandardIcons.listDrillDownAccessoryTexture != null){
					iconImage = new Image(StandardIcons.listDrillDownAccessoryTexture);
				}else{
					iconImage = new Image(Texture.empty(1,1));
				}
				sp.addChild(iconImage);
				button.defaultIcon = sp;
			}
			if(infoList == null){
				infoList = new List();
				addChild(infoList);
			}
//			if(cp == null){
//				cp = new ClippedSprite();
//				cp.addChild(infoList);
//				addChild(cp);
//			}
		}
		public function get dataProvider():ListCollection
		{
			return infoList.dataProvider;
		}
		public function set dataProvider(value:ListCollection):void
		{
			infoList.dataProvider = value;
			infoList.addEventListener(FeathersEventType.RESIZE,onLayer);
			if(_labelField == null)labelField = 'label';
			addListeners();
		}
		
		private var isShowList:Boolean;
		private function addListeners():void
		{
			infoList.addEventListener(Event.CHANGE, listChange);
			button.addEventListener(Event.TRIGGERED,onClickButton);
//			infoList.addEventListener(FeathersEventType.INITIALIZE,onLayer);
//			setTimeout(onLayer,200);
		}
		
		private function onLayer(e:Event = null):void
		{
//			trace('布局变动');
			invalidate();
			dispatchEventWith(FeathersEventType.INITIALIZE);
		}
		
		private function onClickButton(e:Event):void
		{
			if(!isShowList){
				showInfo();
			}else{
				hideInfo();
			}
		}
		
		private function hideInfo():void
		{
			TweenLite.to(infoList,.2,{height:0,onComplete:removeList});
			iconImage.scaleX = 1;//恢复
			iconImage.x = 0;
			isShowList = false;
		}
		
		private function showInfo():void
		{
			addChild(infoList);
			TweenLite.to(infoList,.2,{height:listHeight});
			iconImage.scaleX = -1;//反向处理
			iconImage.x = iconImage.width;
			isShowList = true;
		}
		
		private function removeList():void{
			if(infoList.parent != null)infoList.parent.removeChild(infoList);
		}
		
		private function listChange(e:Event):void
		{
			var ie:ItemEvent = new ItemEvent(ItemEvent.ITEM_CLICK);
			ie.selectedItem = infoList.selectedItem;
			ie.selectedIndex = infoList.selectedIndex;
			setSelectIndex(ie.selectedIndex);
			hideInfo();
			dispatchEvent(ie);
		}
		
		private var _labelField:String;
		public function set labelField(value:String):void
		{
			_labelField = value;
			infoList.itemRendererProperties.labelField = value;
			infoList.itemRendererProperties.horizontalAlign = Button.HORIZONTAL_ALIGN_CENTER;
			infoList.itemRendererProperties.verticalAlign = Button.VERTICAL_ALIGN_MIDDLE;
//			infoList.itemRendererProperties.gap = 0;
			defaultVaule();
		}
		
		private function defaultVaule():void
		{
			setSelectIndex(0);
		}
		
		private function setSelectIndex(index:int):void{
			if(infoList.dataProvider.length > index){
				button.label = infoList.dataProvider.getItemAt(index)[_labelField];
			}
		}
		
//		private var cp:ClippedSprite;
		private var listHeight:Number;
		private function invalidate():void
		{
//			infoList.x = -infoList.width;
//			cp.clipRect = new Rectangle(0,0,infoList.width,infoList.height);
//			cp.touchable = true;
//			cp.x = button.width;
			infoList.removeEventListener(FeathersEventType.RESIZE,onLayer);
			button.width = infoList.width + iconImage.width;
			infoList.x = button.width;
			listHeight = infoList.height;
			infoList.height = 0;
		}		
			
		
		
		
		
	}
}